package com.acoreful.bootsample.init;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.acoreful.bootsample.domain.City;
import com.acoreful.bootsample.domain.Country;
import com.acoreful.bootsample.service.BrandStoreService;
import com.acoreful.bootsample.service.RedisCacheUtil;

import lombok.extern.slf4j.Slf4j;

/*
 * 监听器，用于项目启动的时候初始化信息
 */
@Service
@Slf4j
public class StartInitCacheListener implements InitializingBean
{
    //日志
    
    @Autowired
    private RedisCacheUtil<Object> redisCache;
    
    @Autowired
    private BrandStoreService brandStoreService;
    

	@Override
	public void afterPropertiesSet() throws Exception {
		//spring 启动的时候缓存城市和国家等信息
		log.info("\n\n\n_________\n\n缓存数据 \n\n ________\n\n\n\n");
		List<City> cityList = brandStoreService.selectAllCityMessage();
		List<Country> countryList = brandStoreService.selectAllCountryMessage();

		Map<Long, City> cityMap = new HashMap<Long, City>();

		Map<Long, Country> countryMap = new HashMap<Long, Country>();

		int cityListSize = cityList.size();
		int countryListSize = countryList.size();

		for (int i = 0; i < cityListSize; i++) {
			cityMap.put(cityList.get(i).getCityId(), cityList.get(i));
		}

		for (int i = 0; i < countryListSize; i++) {
			countryMap.put(countryList.get(i).getCountryId(), countryList.get(i));
		}

		redisCache.setCacheIntegerMap("cityMap", cityMap);
		redisCache.setCacheIntegerMap("countryMap", countryMap);
		// TODO Auto-generated method stub
		
	}
    
}